package com.by.course.serivce.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.course.entity.base.BaseResult;
import com.by.course.entity.base.PageResult;
import com.by.course.entity.bo.BaseCourse;
import com.by.course.entity.bo.BaseLesson;
import com.by.course.entity.pojo.LessonInfo;
import com.by.course.entity.vo.course.req.AddBaseCourseVO;
import com.by.course.entity.vo.course.req.DeleteBaseCourseVO;
import com.by.course.entity.vo.course.req.GetBaseCourseVO;
import com.by.course.entity.vo.course.req.UpdateBaseCourseVO;
import com.by.course.entity.vo.course.resp.BaseCourseResultVO;
import com.by.course.entity.vo.lesson.req.AddBaseLessonVO;
import com.by.course.entity.vo.lesson.req.DeleteBaseLessonVO;
import com.by.course.entity.vo.lesson.req.GetBaseLessonVO;
import com.by.course.entity.vo.lesson.req.UpdateBaseLessonVO;
import com.by.course.entity.vo.lesson.resp.BaseLessonResultVO;
import com.by.course.enums.CommonStatusEnum;
import com.by.course.enums.ErrEnum;
import com.by.course.execption.BusinessException;
import com.by.course.mapper.BaseCourseMapper;
import com.by.course.mapper.BaseLessonMapper;
import com.by.course.serivce.BaseCourseService;
import com.by.course.serivce.BaseLessonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.by.course.entity.base.PageResult.genEmptyData;

@Service
public class BaseCourseServiceImpl extends ServiceImpl<BaseCourseMapper, BaseCourse> implements BaseCourseService {

    @Resource
    private BaseCourseMapper baseCourseMapper;

    @Resource
    private BaseLessonMapper baseLessonMapper;

    @Resource
    private BaseCourseService baseCourseService;

    @Resource
    private BaseLessonService baseLessonService;

    @Override
    public BaseResult<String> createCourse(AddBaseCourseVO vo) {
        if (vo.getTeacherId() == null) {
            throw new BusinessException("课程老师不能为空", ErrEnum.COURSE_ERR.getCode());
        }
        BaseCourse baseCourse = new BaseCourse();
        BeanUtil.copyProperties(vo, baseCourse);
        baseCourseMapper.insertSelective(baseCourse);
        return BaseResult.builder().build();
    }

    @Override
    public BaseResult<String> updateCourse(UpdateBaseCourseVO vo) {
        if (vo.getId() == null) {
            throw new BusinessException("请求参数错误", ErrEnum.COURSE_ERR.getCode());
        }
        BaseCourse baseCourse = baseCourseMapper.selectById(vo.getId());
        if (baseCourse == null) {
            throw new BusinessException("没有对应课程", ErrEnum.COURSE_ERR.getCode());
        }
        if (vo.getTeacherId() != null) {
            baseCourse.setTeacherId(vo.getTeacherId());
        }
        if (StrUtil.isNotBlank(vo.getName())) {
            baseCourse.setName(vo.getName());
        }
        if (StrUtil.isNotBlank(vo.getDescription())) {
            baseCourse.setDescription(vo.getDescription());
        }
        if (vo.getDuration() != null) {
            baseCourse.setDuration(vo.getDuration());
        }
        if (vo.getPrice() != null) {
            baseCourse.setPrice(vo.getPrice());
        }
        baseCourseMapper.updateByPrimaryKeySelective(baseCourse);
        return BaseResult.builder().build();
    }

    @Override
    public BaseResult<String> deleteCourse(DeleteBaseCourseVO vo) {
        if (vo.getId() == null) {
            throw new BusinessException("请求参数错误", ErrEnum.COURSE_ERR.getCode());
        }
        BaseCourse baseCourse = baseCourseMapper.selectById(vo.getId());
        if (baseCourse == null) {
            throw new BusinessException("没有对应课程", ErrEnum.COURSE_ERR.getCode());
        }
        baseCourse.setStatus(CommonStatusEnum.CLOSE.getCode());
        baseCourseMapper.updateByPrimaryKeySelective(baseCourse);
        return BaseResult.builder().build();
    }

    @Override
    public BaseResult<BaseCourseResultVO> getCourseById(GetBaseCourseVO vo) {
        if (vo.getId() == null) {
            throw new BusinessException("请求参数错误", ErrEnum.COURSE_ERR.getCode());
        }
        BaseCourse baseCourse = baseCourseMapper.selectById(vo.getId());
        BaseCourseResultVO baseCourseResultVO = new BaseCourseResultVO();
        BeanUtil.copyProperties(baseCourse, baseCourseResultVO);
        List<BaseLesson> baseLessonList = baseLessonMapper.selectByCourseId(baseCourse.getId());
        if (baseLessonList != null && !baseLessonList.isEmpty()) {
            List<LessonInfo> lessonList = baseLessonList.stream().map(baseLesson -> {
                LessonInfo lessonInfo = new LessonInfo();
                BeanUtil.copyProperties(baseLesson, lessonInfo);
                return lessonInfo;
            }).collect(Collectors.toList());
            baseCourseResultVO.setLessonList(lessonList);
        }
        return new BaseResult.Builder<BaseCourseResultVO>().setData(baseCourseResultVO).build();
    }

    @Override
    public BaseResult<PageResult<BaseCourseResultVO>> queryCourses(GetBaseCourseVO vo) {
        if (vo.getPageSize() == null) {
            vo.setPageSize(10);
        }
        if (vo.getCurrentPage() == null) {
            vo.setCurrentPage(1);
        }
        vo.setOffset((vo.getCurrentPage() - 1) * vo.getPageSize());
        Integer count = baseCourseMapper.countQueryCourses(vo);
        if (count == null || count == 0) {
            return new BaseResult.Builder<PageResult<BaseCourseResultVO>>().setData(genEmptyData()).build();
        }
        List<BaseCourseResultVO> baseCourseResultVoList = baseCourseMapper.queryCourses(vo);
        List<Long> courseIdList = baseCourseResultVoList.stream().map(BaseCourseResultVO::getId).collect(Collectors.toList());
        List<BaseLesson> baseLessonList = baseLessonMapper.selectLessonTitleByCourseIdList(courseIdList);
        Map<Long, List<BaseLesson>> lessonMap = baseLessonList.stream().collect(Collectors.groupingBy(BaseLesson::getCourseId));
        baseCourseResultVoList.forEach(baseCourseResultVO -> {
                    List<BaseLesson> lessonList = lessonMap.get(baseCourseResultVO.getId());
                    if (CollectionUtil.isNotEmpty(lessonList)) {
                        List<LessonInfo> lessonInfoList = lessonList.stream().map(baseLesson -> {
                            LessonInfo lessonInfo = new LessonInfo();
                            BeanUtil.copyProperties(baseLesson, lessonInfo);
                            return lessonInfo;
                        }).collect(Collectors.toList());
                        baseCourseResultVO.setLessonList(lessonInfoList);
                    }
                });
        PageResult<BaseCourseResultVO> resultPage = new PageResult<>(baseCourseResultVoList, count, vo.getCurrentPage(), vo.getPageSize());
        return new BaseResult.Builder<PageResult<BaseCourseResultVO>>().setData(resultPage).build();
    }

    @Override
    public BaseResult<String> createLesson(AddBaseLessonVO vo) {
        if (vo.getCourseId() == null) {
            throw new BusinessException("章节ID不能为空", ErrEnum.LESSON_ERR.getCode());
        }
        BaseLesson baseLesson = new BaseLesson();
        BeanUtil.copyProperties(vo, baseLesson);
        baseLessonMapper.insertSelective(baseLesson);
        return BaseResult.builder().build();
    }

    @Override
    public BaseResult<String> updateLesson(UpdateBaseLessonVO vo) {
        if (vo.getId() == null) {
            throw new BusinessException("请求参数错误", ErrEnum.LESSON_ERR.getCode());
        }
        BaseLesson baseLesson = baseLessonMapper.selectById(vo.getId());
        if (baseLesson == null) {
            throw new BusinessException("没有对应章节", ErrEnum.LESSON_ERR.getCode());
        }
        if (vo.getCourseId() != null) {
            baseLesson.setCourseId(vo.getCourseId());
        }
        if (StrUtil.isNotBlank(vo.getTitle())) {
            baseLesson.setTitle(vo.getTitle());
        }
        if (vo.getSequence() != null) {
            baseLesson.setSequence(vo.getSequence());
        }
        if (vo.getDuration() != null) {
            baseLesson.setDuration(vo.getDuration());
        }
        if (StrUtil.isNotBlank(vo.getDescription())) {
            baseLesson.setDescription(vo.getDescription());
        }
        baseLessonMapper.updateByPrimaryKeySelective(baseLesson);
        return BaseResult.builder().build();
    }

    @Override
    public BaseResult<String> deleteLesson(DeleteBaseLessonVO vo) {
        if (vo.getId() == null) {
            throw new BusinessException("请求参数错误", ErrEnum.LESSON_ERR.getCode());
        }
        BaseLesson baseLesson = baseLessonMapper.selectById(vo.getId());
        if (baseLesson == null) {
            throw new BusinessException("没有对应章节", ErrEnum.LESSON_ERR.getCode());
        }
        baseLessonMapper.deleteById(baseLesson.getId());
        return BaseResult.builder().build();
    }

    @Override
    public BaseResult<BaseLessonResultVO> getLessonById(GetBaseLessonVO vo) {
        if (vo.getId() == null) {
            throw new BusinessException("请求参数错误", ErrEnum.LESSON_ERR.getCode());
        }
        BaseLesson baseLesson = baseLessonMapper.selectById(vo.getId());
        BaseLessonResultVO baseLessonResultVO = new BaseLessonResultVO();
        BeanUtil.copyProperties(baseLesson, baseLessonResultVO);
        return new BaseResult.Builder<BaseLessonResultVO>().setData(baseLessonResultVO).build();
    }
}
